package com.alan.controller;

import com.alan.entity.admin;
import com.alan.entity.student;
import com.alan.entity.teach;
import com.alan.service.AdminService;
import com.alan.service.LoginService;
import com.alan.service.RegisterService;
import com.alan.service.TeacherService;
import com.alan.service.impl.AdminServiceImpl;
import com.alan.service.impl.LoginServiceImpl;
import com.alan.service.impl.RegisterServiceImpl;
import com.alan.service.impl.TeacherServiceIpml;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private LoginService LoginService = new LoginServiceImpl();
    private TeacherService teacherService = new TeacherServiceIpml();
    private AdminService adminService = new AdminServiceImpl();
    private RegisterService registerService=new RegisterServiceImpl();

    /**
     * 处理登录的业务逻辑
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String identify = req.getParameter("identify");
        Object object = LoginService.login(username, password, identify);
        String re = registerService.find(username,identify);
        if (object != null) {
            HttpSession session = req.getSession();
            switch (identify) {
                case "student":
                    student student = (student) object;
                    session.setAttribute("student", student);
                    req.getRequestDispatcher("success.jsp?info=student&name=" + student.getName()).forward(req, resp);
                    break;
                case "teacher":
                    teach teach = (teach) object;
                    session.setAttribute("teach", teach);
                    System.out.println(teach.getName());
                    req.getRequestDispatcher("success.jsp?info=teacher&name=" + teach.getName()).forward(req, resp);
                    break;
                case "admin":
                    admin admin = (admin) object;
                    session.setAttribute("admin", admin);
                    req.getRequestDispatcher("success.jsp?info=admin&name=" + admin.getName()).forward(req, resp);
                    break;
            }
        } else {
            if (re == "null") {
                req.setAttribute("info", "nohaved");
                req.getRequestDispatcher("fail.jsp").forward(req, resp);
            } else {
                req.setAttribute("info", "infofail");
                req.getRequestDispatcher("fail.jsp").forward(req, resp);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
